using UnityEngine;
using Weapons.Components.ComponentData;
using Weapons.Components.ComponentData.AttackData;

namespace Weapons.Components
{
    public class KnockBack : WeaponComponent<KnockBackData, AttackKnocBack>
    {
        private ActionHitBox hitBox;

        private Movement movement;


        protected override void Start()
        {
            base.Start();

            movement = Core.GetCoreComponent<Movement>();
            hitBox = GetComponent(typeof(ActionHitBox)) as ActionHitBox;
            hitBox.OnDetectedCollider2D += HandleDetectCollider2D;
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();

            hitBox.OnDetectedCollider2D -= HandleDetectCollider2D;
        }

        private void HandleDetectCollider2D(Collider2D[] colliders)
        {
            foreach (var collider in colliders)
                if (collider.TryGetComponent(out IKnockBackable knockBackAble))
                    knockBackAble.KnockBack(currentAttackData.Angle, currentAttackData.Strength,
                        movement.FacingDirection);
        }
    }
}